package com.springboot.test.common.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

public class MybatisPlusGenerator {
    public static void main(String[] args) {
        // 模块名
        String model = "";
        //todo 表名修改
        String[] tableArr = {"sm_promotion_full_discount","sm_promotion_full_discount_goods","sm_promotion_full_discount_member","sm_promotion_full_discount_rule"};

// ----------------------------------------------------------------------------------------------------------

        AutoGenerator mpg = new AutoGenerator();
        // 选择 freemarker 引擎，默认 Veloctiy
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        // 全局配置
        GlobalConfig gc = new GlobalConfig();

        gc.setOutputDir("/Users/yangfei/Desktop/db/sm");
        gc.setFileOverride(true);
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setActiveRecord(false);// 关闭AR模式
        gc.setAuthor("sm"); // 设置作者
//        gc.setServiceName("%sService"); // 生成的service接口名字首字母是否为I，这样设置就没有I
        gc.setEntityName("%sEntity");
        gc.setServiceImplName("%sService");
        gc.setBaseResultMap(false);
        gc.setIdType(IdType.AUTO);
        gc.setDateType(DateType.ONLY_DATE);

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("ems_dev");
        dsc.setPassword("ems_dev123");
        // TODO 2 库名修改
        dsc.setUrl("jdbc:mysql://rm-bp1cl465ovjrf72dl6o.mysql.rds.aliyuncs.com:3306/sm-dev-promotion?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8");
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude(tableArr); // 设置需要生成代码的多个完整表名
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
//        strategy.setSuperEntityClass("com.dj.note.base.BaseEntity");
//        strategy.setSuperControllerClass("com.dj.note.base.BaseController");
        strategy.setRestControllerStyle(true);
        strategy.setEntityLombokModel(true);
        strategy.setEntityTableFieldAnnotationEnable(true);
        // 写于父类中的公共字段,一下为生成的实体所忽略的字段
//        strategy.setSuperEntityColumns("create_user_id", "update_user_id", "create_time", "update_time");

        strategy.setTablePrefix("");


        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.toprisers.promotion"); // TODO 3 包名修改
        pc.setMapper("dal.mapper").setService("service").setController("web.controller").setEntity("entity").setXml("mapper");// 设置xml文件的目录
        mpg.setPackageInfo(pc);
        pc.setModuleName(model);

        // 执行生成
        mpg.execute();
        System.out.println("执行成功!");
    }
}
